package com.tongfusoft.document.manager.securityGroup.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.da.template.service.impl.DataTemplateServiceImpl;
import com.tongfusoft.document.entity.Docs;
import com.tongfusoft.document.entity.RoleHasSecurities;
import com.tongfusoft.document.entity.Roles;
import com.tongfusoft.document.entity.SecurityGroup;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.role.service.IDocumentRoleService;
import com.tongfusoft.document.manager.security.service.IDocumentSecurityGroupService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("documentSecurityGroupService")
public class DocumentSecurityGroupServiceImpl extends
		DataTemplateServiceImpl implements IDocumentSecurityGroupService {
	@Autowired
	@Qualifier(value="DocumentRoleService")
	public IDocumentRoleService roleService;
	@Autowired
	@Qualifier(value="documentSecurityGroupService")
	public IDocumentSecurityGroupService groupService;
	/**
	 * 创建安全组
	 * */
	@Override
	public void createSecurityGroup(SecurityGroup group, Users user) {
		if (super.selectList("groupList", group).size() > 0) {

			throw new ServiceException("repeat_error");
		}
		group.setCreatedBy(user.getUserId());
		group.setCreatedDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		group.setLastUpdBy(user.getUserId());
		group.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		group.setGroupId(UIDGeneratorService.getUUID());
		group.setStatus("1");
		
		Roles role = new Roles();
		List<Roles> allRole = this.roleService.roleList(role);
		RoleHasSecurities roleHasSecurities = new RoleHasSecurities();
		for (int i = 0; i < allRole.size(); i++) {
			roleHasSecurities.setGroupId(group.getGroupId());
			roleHasSecurities.setRoleId(allRole.get(i).getRoleId());
			this.groupService.addRoleHasGroup(roleHasSecurities,user);
		}

		this.insert("addGroup",group);
	}
	/**
	 * 删除安全组前判断安全组是否已被使用
	 * */
	@Override
	public List<Docs> isGroupByDoc(String groupId) {
		return this.selectList("isGroupByDoc", groupId);
	}
	
	/**
	 * 删除安全组
	 * */
	@Override
	public void deleteSecurityGroup(SecurityGroup group, Users user) {
		RoleHasSecurities roleHasSecurities = new RoleHasSecurities();
		roleHasSecurities.setGroupId(group.getGroupId());
		this.groupService.deleteRoleHasSecurities(roleHasSecurities, user);
		
		group.setLastUpdBy(user.getUserId());
		group.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		this.delete("delGroup", group);
	}
	/**
	 * 编辑安全组
	 * */
	@Override
	public void updateSecurityGroup(SecurityGroup group, Users user) {
		if (super.selectList("groupListOutSelf", group).size() > 0) {

			throw new ServiceException("repeat_error");
		}
		group.setLastUpdBy(user.getUserId());
		group.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		this.update("updateGroup", group);
	}
	/**
	 * 根据角色id来查询其对应权限
	 * */
	@Override
	public RoleHasSecurities powerByRpId(String rpId) {
		return (RoleHasSecurities) this.selectOne("allRoleHasSecurities", rpId);
	}
	@Override
	public void updatePower(RoleHasSecurities privileges, Users user) {
		// TODO Auto-generated method stub

	}
	/**
	 * 删除安全组对应的权限
	 * */
	@Override
	public void deleteRoleHasSecurities(RoleHasSecurities roleHasSecurities, Users user) {
		roleHasSecurities.setLastUpdBy(user.getUserId());
		roleHasSecurities.setLastUpdDate(new Timestamp(new Date().getTime()));
		this.delete("delRoleHasGroup", roleHasSecurities);
	}
	/**
	 * 安全组列表（分页）
	 * */
	@Override
	public PageInfo<SecurityGroup> groupPageTable(int pageIndex, int pageSize,
			SecurityGroup group) {
		return super.findPage("groupList", group, pageSize, pageIndex);
	}
	/**
	 * 安全组详细
	 * */
	@Override
	public SecurityGroup getSecurityGroup(SecurityGroup group) {
		return (SecurityGroup) this.selectOne("getGroup", group);
	}
	/**
	 * 安全组列表
	 * */
	@Override
	public List<SecurityGroup> groupList(SecurityGroup group) {
		return this.selectList("groupList",group);
	}
	
	/**
	 * 修改对应角色与安全组权限
	 * */
	@Override
	public void updateRoleHasGroup(RoleHasSecurities roleHasSecurities ,Users user) {
		roleHasSecurities.setLastUpdBy(user.getUserId());
		roleHasSecurities.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		this.update("updateRoleHasGroup", roleHasSecurities);
	}

	/**
	 * 角色与安全组绑定
	 * */
	@Override
	public void addRoleHasGroup(RoleHasSecurities roleHasSecurities,Users user) {

		roleHasSecurities.setRpId(UIDGeneratorService.getUUID());
		roleHasSecurities.setReadPower("0");
		roleHasSecurities.setDeletePower("0");
		roleHasSecurities.setUpdatePower("0");
		roleHasSecurities.setManagerPower("0");
		roleHasSecurities.setCreatedBy(user.getUserId());
		roleHasSecurities.setCreatedDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		roleHasSecurities.setLastUpdBy(user.getUserId());
		roleHasSecurities.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		roleHasSecurities.setStatus("1");
		this.insert("addRoleHasGroup", roleHasSecurities);
	}
	/**
	 * 角色与安全组权限管理表
	 * */
	@Override
	public PageInfo<RoleHasSecurities> roleHasSecuritiesPageTable(
			int pageIndex, int pageSize, RoleHasSecurities roleHasSecurities){
		return super.findPage("roleHasGroupList", roleHasSecurities, pageSize, pageIndex);
	}
	/**
	 * 根据groupId来删除对应的权限
	 * */
	@Override
	public void delRoleHasGroup(String groupId) {
		this.delete("delRoleHasGroup", groupId);
	}

}
